import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/res/decoration.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/widget/image/local_image.dart';

Widget roundWidget({
  Widget? child,
  double? width,
  double? height,
  EdgeInsetsGeometry? margin,
  Clip clipBehavior = Clip.none,
  EdgeInsetsGeometry? padding,
  AlignmentGeometry? alignment = Alignment.center,
  Decoration? decoration,
}) {
  return Container(
      clipBehavior: clipBehavior,
      margin: margin,
      padding: padding,
      alignment: alignment,
      width: width,
      height: height,
      decoration: decoration,
      child: child);
}

///正方形圆角图片
Widget roundLocalImage(
  String image, {
  double radius = 8,
  EdgeInsetsGeometry? margin,
  EdgeInsetsGeometry? padding,
  Decoration? decoration,
  String? package,
}) {
  return roundWidget(
      margin: margin,
      padding: padding,
      clipBehavior: Clip.hardEdge,
      decoration: decoration ?? rs8,
      width: radius * 2,
      child: LocalImage(
        image,
        package: package,
        width: radius * 2,
        height: radius * 2,
      ));
}

Widget inputInfo({
  BorderRadiusGeometry borderRadius =
      const BorderRadius.all(Radius.circular(6.0)),
  TextEditingController? controller,
  TextInputType? keyboardType = TextInputType.number,
  String unit = '',
  String? hintText,
  bool enabled = true,
  Color? color,
  double? width = double.infinity,
  double? height,
  FocusNode? focusNode,
  BoxDecoration? decoration,
  List<TextInputFormatter>? inputFormatters,
  ValueChanged<String>? onChanged,
}) {
  return GestureDetector(
    onTap: () {
      focusNode?.requestFocus();
    },
    child: Container(
        alignment: Alignment.center,
        width: width,
        height: height,
        decoration: decoration??BoxDecoration(
          color: color ?? ThemeUtil.getTheme().secondaryBgColor(),
          borderRadius: borderRadius,
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Expanded(
              child: Container(
                padding: EdgeInsets.symmetric(vertical: 30.w, horizontal: 24.w),
                alignment: Alignment.center,
// width: double.infinity,
                child: TextField(
                  focusNode: focusNode,
                  style: enabled
                      ? TextStyles.textNormal28
                      : TextStyles.textSixthColor28,
                  enabled: enabled,
                  inputFormatters: inputFormatters,
                  onChanged: onChanged,
                  autofocus: false,
                  controller: controller,
                  keyboardType: keyboardType,
                  textAlignVertical: TextAlignVertical.center,
                  decoration: InputDecoration(
                    //下面border都是为了移除内置的padding
                    focusedBorder: const OutlineInputBorder(
                        borderSide:
                            BorderSide(width: 0, color: Colors.transparent)),
                    disabledBorder: const OutlineInputBorder(
                        borderSide:
                            BorderSide(width: 0, color: Colors.transparent)),
                    enabledBorder: const OutlineInputBorder(
                        borderSide:
                            BorderSide(width: 0, color: Colors.transparent)),
                    contentPadding:
                        const EdgeInsets.symmetric(vertical: 0, horizontal: 0),
                    border: InputBorder.none,
                    //这个属性是为了移除TextField额外的高度
                    isDense: true,
                    labelStyle: TextStyles.textNormal28,
                    hintText: hintText,
                    hintStyle: TextStyles.textSixthColor28,
                  ),
                ),
              ),
            ),
            Visibility(
              visible: unit.isNotEmpty,
              child: Container(
                color: ThemeUtil.getTheme().dividingLineColor(),
                width: 2.w,
                height: 50.w,
              ),
            ),
            Visibility(
              visible: unit.isNotEmpty,
              child: Container(
                  alignment: Alignment.center,
                  width: 120.w,
                  child: Text(
                    unit,
                    style: TextStyles.textSixthColor28,
                  )),
            ),
          ],
        )),
  );
}


Widget buildInput({
  TextEditingController? controller,
  TextInputType? keyboardType = TextInputType.number,
  FocusNode? focusNode,
  bool enabled = true,
  String? hintText,
  double? width,
  double? height,
  EdgeInsetsGeometry? padding,
  EdgeInsetsGeometry? margin,
  List<TextInputFormatter>? inputFormatters,
  ValueChanged<String>? onChanged,
  TextAlign? textAlign,
}) {
  return GestureDetector(
    onTap: (){
      focusNode?.requestFocus();
    },
    child: Container(
        width: width,
        height: height,
        margin: margin??EdgeInsets.symmetric(horizontal: 8.w),
        padding: padding,
        decoration: enabled?
        BoxDecoration(
            color: ThemeUtil.getTheme().colorScheme.secondary,
            borderRadius: const BorderRadius.all(Radius.circular(30.0)),
            border: Border.all(
                color: ThemeUtil.getTheme().sixthTextColor(), width: 0.7)):
        BoxDecoration(
            color: ThemeUtil.getTheme().secondaryBgColor(),
            borderRadius: const BorderRadius.all(Radius.circular(30.0)),
            border: Border.all(
                color: ThemeUtil.getTheme().sixthTextColor(), width: 0.7)),
        alignment: Alignment.center,
        child: TextField(
          style: TextStyles.textNormal28,
          keyboardType: keyboardType,
          onChanged: onChanged,
          enabled: enabled,
          autofocus: false,
          controller: controller,
          textAlign: textAlign??TextAlign.center,
          focusNode: focusNode,
          decoration: InputDecoration(
            isDense: true,
            focusedBorder: const OutlineInputBorder(
                borderSide: BorderSide(width: 0, color: Colors.transparent)),
            disabledBorder: const OutlineInputBorder(
                borderSide: BorderSide(width: 0, color: Colors.transparent)),
            enabledBorder: const OutlineInputBorder(
                borderSide: BorderSide(width: 0, color: Colors.transparent)),
            contentPadding: const EdgeInsets.symmetric(vertical: 0),
            border: InputBorder.none,
            labelStyle: normalTextColor28,
            hintText: hintText,
            hintStyle: sixthTextColor28,
          ),
        )),
  );
}